/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.headerfooter.songhang.library.component;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;

import java.util.ArrayList;
import java.util.LinkedList;

/**
 * 回收类
 */
public class Recycler {
    /**
     * 向上滑动标识
     */
    public static final int DIRECTION_UP = 0;

    /**
     * 向下滑动标识
     */
    public static final int DIRECTION_DOWN = 2;

    private ArrayList<RecyclerItem> mAllScrap = new ArrayList<>();
    private LinkedList<RecyclerItem> mAttachedScrap = new LinkedList<>();
    private LinkedList<Component> mCacheScrap = new LinkedList<Component>();
    private BaseItemProvider mProvider;
    private SpanLayout mSpanLayout;

    /**
     * 构造方法
     * @param layout SpanLayout对象
     * @param provider BaseItemProvider对象
     */
    public Recycler(SpanLayout layout, BaseItemProvider provider) {
        this.mSpanLayout = layout;
        this.mProvider = provider;
    }

    /**
     * 获取所有的item
     *
     * @return ArrayList<RecyclerItem>
     */
    public ArrayList<RecyclerItem> getAllScrap() {
        return mAllScrap;
    }

    /**
     * 获取有效的item
     *
     * @return LinkedList<RecyclerItem>
     */
    public LinkedList<RecyclerItem> getAttachedScrap() {
        return mAttachedScrap;
    }

    /**
     * item缓存
     *
     * @param index 序号
     * @param item RecyclerItem对象
     */
    public void cacheItem(int index, RecyclerItem item) {
        mAllScrap.add(index, item);
    }

    /**
     * 添加item
     *
     * @param item RecyclerItem对象
     */
    public void attachComponent(RecyclerItem item) {
        mAttachedScrap.add(item);
    }

    /**
     * 获取组件
     *
     * @param index 序号
     * @param container 组件容器
     * @return Component
     */
    public Component getView(int index, ComponentContainer container) {
        Component cache = mCacheScrap.poll();
        return mProvider.getComponent(index, cache, container);
    }

    /**
     * 添加item
     *
     * @param index 序号
     * @param item RecyclerItem对象
     */
    public void addItem(int index, RecyclerItem item) {
        mAllScrap.add(index, item);
    }

    /**
     * 在尾部添加item
     *
     * @param item RecyclerItem对象
     */
    public void attachItemToEnd(RecyclerItem item) {
        mAttachedScrap.add(item);
    }

    /**
     * 在头部添加item
     *
     * @param item RecyclerItem对象
     */
    public void attachItemToStart(RecyclerItem item) {
        mAttachedScrap.add(0, item);
    }

    /**
     * 释放item
     *
     * @param item RecyclerItem对象
     */
    public void recycle(RecyclerItem item) {
        mSpanLayout.removeComponent(item.child);
        mAttachedScrap.remove(item);
        mCacheScrap.push(item.child);
        item.child = null;
    }

    /**
     * 底部添加item
     *
     * @param item RecyclerItem对象
     */
    public void recoverToEnd(RecyclerItem item) {
        Component child = mProvider.getComponent(item.index, mCacheScrap.poll(), mSpanLayout);
        child.estimateSize(
                Component.EstimateSpec.getSizeWithMode(item.width, Component.EstimateSpec.PRECISE),
                Component.EstimateSpec.getSizeWithMode(item.height, Component.EstimateSpec.PRECISE)
        );
        item.child = child;
        mAttachedScrap.add(item);
        mSpanLayout.addComponent(child);
    }

    /**
     * 头部添加item
     *
     * @param item 对象
     */
    public void recoverToStart(RecyclerItem item) {
        Component child = mProvider.getComponent(item.index, mCacheScrap.poll(), mSpanLayout);
        child.estimateSize(
                Component.EstimateSpec.getSizeWithMode(item.width, Component.EstimateSpec.PRECISE),
                Component.EstimateSpec.getSizeWithMode(item.height, Component.EstimateSpec.PRECISE)
        );
        item.child = child;
        mAttachedScrap.add(0, item);
        mSpanLayout.addComponent(child);
    }
}
